package com.ruoyi.jjyh.controller;

import java.util.List;

import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.jjyh.domain.JjyhMassage;
import com.ruoyi.jjyh.service.IJjyhMassageService;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jjyh.domain.JjyhUser;
import com.ruoyi.jjyh.service.IJjyhUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author ruoyi
 * @date 2020-11-17
 */
@Controller
@RequestMapping("/jjyh/user")
public class JjyhUserController extends BaseController
{
    private String prefix = "jjyh/user";

    @Autowired
    private IJjyhUserService jjyhUserService;
    @Autowired
    private IJjyhMassageService jjyhMassageService;

    @RequiresPermissions("jjyh:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    @RequiresPermissions("jjyh:user:view")
    @GetMapping("/userCheck")
    public String userCheck()
    {
        return prefix + "/userCheck";
    }

    @RequiresPermissions("jjyh:user:view")
    @GetMapping("/userHonestyCheck")
    public String userHonestyCheck()
    {
        return prefix + "/userHonestyCheck";
    }

    /**
     * 查询用户列表
     */
    @RequiresPermissions("jjyh:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JjyhUser jjyhUser)
    {
        startPage();
        List<JjyhUser> list = jjyhUserService.selectJjyhUserList(jjyhUser);
        return getDataTable(list);
    }

    /**
     * 查询用户身份认证申请列表
     */
    @RequiresPermissions("jjyh:user:list")
    @PostMapping("/listUserCheck")
    @ResponseBody
    public TableDataInfo listUserCheck(JjyhUser jjyhUser)
    {
        startPage();
        jjyhUser.setIsCheck("审核中");
        List<JjyhUser> list = jjyhUserService.selectJjyhUserList(jjyhUser);
        return getDataTable(list);
    }

    /**
     * 查询用户诚信认证申请列表
     */
    @RequiresPermissions("jjyh:user:list")
    @PostMapping("/listUserHonestyCheck")
    @ResponseBody
    public TableDataInfo listUserHonestyCheck(JjyhUser jjyhUser)
    {
        startPage();
        jjyhUser.setIsHonestyCheck("申请");
        List<JjyhUser> list = jjyhUserService.selectJjyhUserList(jjyhUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("jjyh:user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JjyhUser jjyhUser)
    {
        List<JjyhUser> list = jjyhUserService.selectJjyhUserList(jjyhUser);
        ExcelUtil<JjyhUser> util = new ExcelUtil<JjyhUser>(JjyhUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户
     */
    @RequiresPermissions("jjyh:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JjyhUser jjyhUser)
    {
        return toAjax(jjyhUserService.insertJjyhUser(jjyhUser));
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JjyhUser jjyhUser = jjyhUserService.selectJjyhUserById(id);
        mmap.put("jjyhUser", jjyhUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("jjyh:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JjyhUser jjyhUser)
    {
        return toAjax(jjyhUserService.updateJjyhUser(jjyhUser));
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("jjyh:user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jjyhUserService.deleteJjyhUserByIds(ids));
    }

    /**
     * 拒绝
     */
    @GetMapping("/review/{id}")
    public String editReview(@PathVariable("id") String id, ModelMap mmap)
    {
        JjyhUser jjyhUser = jjyhUserService.selectJjyhUserById(id);
        mmap.put("jjyhUser", jjyhUser);
        return prefix + "/review";
    }

    /**
     * 审批身份认证
     */
    @RequiresPermissions("jjyh:user:edit")
    @Log(title = "审批身份认证", businessType = BusinessType.UPDATE)
    @PostMapping("/reviewUser")
    @ResponseBody
    public AjaxResult reviewUser(String id)
    {
        JjyhUser temp = new JjyhUser();
        temp.setId(id);
        temp.setIsCheck("审核通过");
        return toAjax(jjyhUserService.updateJjyhUser(temp));
    }

    /**
     * 拒绝身份认证
     */
    @RequiresPermissions("lm:userreview:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/reviewUserDis")
    @ResponseBody
    public AjaxResult reviewUserDis(JjyhUser jjyhUser)
    {
        JjyhUser temp = new JjyhUser();
        temp.setId(jjyhUser.getId());
        temp.setIsCheck("审核不通过");
        temp.setDisReason(jjyhUser.getDisReason());
        int num = jjyhUserService.updateJjyhUser(temp);
        if(num >0){
            //添加拒绝消息提醒
            JjyhMassage jjyhMassage = new JjyhMassage();
            jjyhMassage.setType("2");
            jjyhMassage.setUserId(jjyhUser.getId());
            jjyhMassage.setContent(jjyhUser.getDisReason());
            jjyhMassage.setTitle("身份认证失败");
            jjyhMassageService.insertJjyhMassage(jjyhMassage);
        }
        return toAjax(num);
    }

    /**
     * 拒绝诚信认证
     */
    @GetMapping("/reviewHonesty/{id}")
    public String reviewHonesty(@PathVariable("id") String id, ModelMap mmap)
    {
        JjyhUser jjyhUser = jjyhUserService.selectJjyhUserById(id);
        mmap.put("jjyhUser", jjyhUser);
        return prefix + "/reviewHonesty";
    }

    /**
     * 审批身份认证
     */
    @RequiresPermissions("jjyh:user:edit")
    @Log(title = "审批身份认证", businessType = BusinessType.UPDATE)
    @PostMapping("/reviewUserHonesty")
    @ResponseBody
    public AjaxResult reviewUserHonesty(String id)
    {
        JjyhUser temp = new JjyhUser();
        temp.setId(id);
        temp.setIsHonestyCheck("审核通过");
        return toAjax(jjyhUserService.updateJjyhUser(temp));
    }

    /**
     * 拒绝诚信认证
     */
    @RequiresPermissions("lm:userreview:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/reviewUserDisHonesty")
    @ResponseBody
    public AjaxResult reviewUserDisHonesty(JjyhUser jjyhUser)
    {
        JjyhUser temp = new JjyhUser();
        temp.setId(jjyhUser.getId());
        temp.setIsHonestyCheck("审核不通过");
        temp.setHonestyDisReason(jjyhUser.getHonestyDisReason());
        int num = jjyhUserService.updateJjyhUser(temp);
        if(num >0){
            //添加拒绝消息提醒
            //添加拒绝消息提醒
            JjyhMassage jjyhMassage = new JjyhMassage();
            jjyhMassage.setType("2");
            jjyhMassage.setUserId(jjyhUser.getId());
            jjyhMassage.setContent(jjyhUser.getHonestyDisReason());
            jjyhMassage.setTitle("诚信认证失败");
            jjyhMassageService.insertJjyhMassage(jjyhMassage);
        }
        return toAjax(num);
    }

    /**
     * 用户封号
     */
    @RequiresPermissions("lm:user:edit")
    @Log(title = "用户封号", businessType = BusinessType.UPDATE)
    @PostMapping("/disableUser")
    @ResponseBody
    public AjaxResult disableUser(String id)
    {
        JjyhUser temp = new JjyhUser();
        temp.setId(id);
        temp.setIsDisable("0");
        return toAjax(jjyhUserService.updateJjyhUser(temp));
    }

    /**
     * 用户解封
     */
    @RequiresPermissions("lm:user:edit")
    @Log(title = "用户解封", businessType = BusinessType.UPDATE)
    @PostMapping("/enableUser")
    @ResponseBody
    public AjaxResult enableUser(String id)
    {
        JjyhUser temp = new JjyhUser();
        temp.setId(id);
        temp.setIsDisable("1");
        return toAjax(jjyhUserService.updateJjyhUser(temp));
    }

    /**
     * 查看用户
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") String id, ModelMap mmap)
    {
        JjyhUser jjyhUser = jjyhUserService.selectJjyhUserById(id);
        mmap.put("jjyhUser", jjyhUser);
        return prefix + "/view";
    }

}
